/**
 * @file type.c
 * Betriebssysteme Type File.
 *
 * Beispiel 1
 *
 * @author Siegfried Becher 1410258035 <siegfried.becher@technikum-wien.at>
 * @date 2015/03/12
 *
 * @version 1.0
 *
 * Handles the search for Files of a specific type
 *
 */

/*
 * -------------------------------------------------- includes --
 */

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "type.h"

/*
 * --------------------------------------------------- defines --
 */

/*
 * -------------------------------------------------- typedefs --
 */

/*
 * --------------------------------------------------- globals --
 */

/*
 * ------------------------------------------------- functions --
 */

/**
 *
 * \brief check_type function
 *
 * seeks files of a specific type
 *
 * \param argv program arguments for error handling
 * \param name name and path of the current file
 * \param parms type of file for search
 *
 * \return bool
 *
 */

bool check_type(const char * const * argv,char *name,const char *parms)
{
  char *path = name;
  const char *type = parms;
  struct stat buf;

  /* get INode of file */
  if(lstat(path, &buf) == -1)
  {
    /* lstat error */
    fprintf(stderr, "lstat failed: '%s': %s\n", path, strerror(errno));
    return true;
  }
  else
  {
    /** 
     * switch between the various types of files 
     *
     * if the type-parameter is not b, c, d, p, f, l, or s -> error and return false
     */


    switch(type[0])
    {
      printf("This is the mode: %c\n\n", buf.st_mode);
      case 'b': if(S_ISBLK(buf.st_mode)) return true; break;
      case 'c': if(S_ISCHR(buf.st_mode)) return true; break;
      case 'd': if(S_ISDIR(buf.st_mode)) return true; break;
      case 'p': if(S_ISFIFO(buf.st_mode)) return true; break;
      case 'f': if(S_ISREG(buf.st_mode)) return true; break;
      case 'l': if(S_ISLNK(buf.st_mode)) return true; break;
      case 's': if(S_ISSOCK(buf.st_mode)) return true; break;
      default: 
      {
        if(strlen(type) == 1)
        {
          fprintf(stderr, "%s: Unknown argument to -type: %s\n", argv[0], type);
        }
        else
        {
          fprintf(stderr, "%s: Arguments to -type should contain only one letter\n", argv[0]);
        }
        exit(3); return false; break;
      }
    }
  }

  return false;
}

/*
 * ================================================== eof ==
 */
